package com.bosssoft.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.entity.dto.FibAuthDTO;
import com.bosssoft.entity.po.FibAuthPO;
import com.bosssoft.entity.vo.FibAuthVO;
import com.bosssoft.enums.CopyPropertityEnum;
import com.bosssoft.exception.DaoException;
import com.bosssoft.page.PageResult;
import com.bosssoft.response.Result;
import com.bosssoft.service.FibAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 项昊天
 * @create 2021/8/17 20:18
 */
@Slf4j
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Resource
    FibAuthService authService;

    @PostMapping("/save")
    public Result saveAuth(@RequestBody @Validated FibAuthVO authVO) {
        try {
            Byte deleted = 1;
            authVO.setDeleted(deleted);
            FibAuthDTO authDTO = new FibAuthDTO();
            BeanUtils.copyProperties(authVO, authDTO);
            if (authService.addAuth(authDTO)) {
                return Result.ok().message("添加权限成功!");
            }
            return Result.error().message("添加权限失败!权限名已存在!");
        } catch (Exception e) {
            throw DaoException.error().setCodeAndMessage(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }

    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("/getAuthPage")
    public Result getAuthPage(@RequestParam(defaultValue = "1") Integer pageNo,
                              @RequestParam(defaultValue = "10") Integer pageSize, FibAuthVO fibAuthVO) {
        try {
            Page<FibAuthPO> page = new Page<>(pageNo, pageSize);
            QueryWrapper<FibAuthPO> queryWrapper = new QueryWrapper<>();
            if (ObjectUtil.isNotNull(fibAuthVO.getAuthId())) {
                queryWrapper.like("auth_id", fibAuthVO.getAuthId());
            }
            if (ObjectUtil.isNotNull(fibAuthVO.getAuthName())) {
                queryWrapper.like("auth_name", fibAuthVO.getAuthName());
            }
            Page<FibAuthPO> authPage = authService.page(page, queryWrapper);
            long total = authPage.getTotal();
            List<FibAuthPO> recordPOs = authPage.getRecords();
            PageResult<FibAuthPO> results = new PageResult<>(page, recordPOs);
            return Result.ok().data("total", total)
                    .data("page", results)
                    .data("pageNo", pageNo);
        } catch (Exception e) {
            log.info("getAuthPage出现异常");
            throw DaoException.error(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }

    @PutMapping("/modify")
    public Result modifyAuth(@RequestBody @Validated FibAuthVO authVO) {
        try {
            FibAuthDTO authDTO = new FibAuthDTO();
            BeanUtils.copyProperties(authVO, authDTO);
            if (authService.modifyAuth(authDTO)) {
                return Result.ok().message("修改权限成功!");
            }
            return Result.error().message("修改权限失败!");
        } catch (Exception e) {
            log.info("修改权限异常");
            throw DaoException.error().setCodeAndMessage(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }

    @PutMapping("/ban/{id}/{status}")
    public Result banAuth(@PathVariable("id") Long id,
                          @PathVariable("status") Integer status) {
        if (authService.banAuthById(id, status)) {
            return Result.ok().message("禁用权限成功!");
        }
        return Result.error().message("禁用权限失败");
    }

    @DeleteMapping("/delete")
    public Result delAuth(@RequestBody FibAuthVO authVO) {
        if (authVO.getAuthId() != null && authService.deleteAuth(authVO.getAuthId())) {
            return Result.ok().message("删除权限成功");
        }
        return Result.error().message("删除权限失败!");
    }
}
